#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/7/18 21:23
# @Author  : LiShan
# @Email   : lishan_1997@126.com
# @File    : specify_fix_drl_delay.py
# @Note    : this is note
import sys

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

font = {'family' : 'SimSun',
    'weight' : 'bold',
    'size'  : '16'}
plt.rc('font', **font)
plt.rc('axes',unicode_minus=False)
# plt.rcParams['figure.facecolor'] = "#FFFFF0"  # 设置窗体颜色
# plt.rcParams['axes.facecolor'] = "#FFFFF0"  # 设置绘图区颜色

path = sys.path[0].replace("\\", "/")

dqn_record_file = path + "/model/txt/test_drl_record.txt"
fix_record_file = path + "/specify/txt/test_fix_record_1.txt"
compar_img = path + "/specify/png/compare_dqn_fix_delay.png"

if __name__ == '__main__':
    # dqn 延误
    names = ["step", "plan", "delay", "reward"]
    data = pd.read_csv(dqn_record_file, sep="\t+", names=names, engine="python")
    dqn_delay = list(data["delay"].values)
    dqn_plan = list(data["plan"].values)
    dqn_mean_delay = sum(dqn_delay) / len(dqn_delay)
    dqn_mean_delay = round(dqn_mean_delay, 3)

    names = ["step", "delay"]
    data = pd.read_csv(fix_record_file, sep="\t+", names=names, engine="python")
    fix_delay = list(data["delay"].values)
    fix_mean_delay = sum(fix_delay) / len(fix_delay)
    fix_mean_delay = round(fix_mean_delay, 3)

    """dqn、fix测试延误图"""
    # 绘图
    x = np.linspace(0, len(dqn_delay) - 1, len(dqn_delay))
    plt.plot(x, fix_delay, color='gray', marker='*', linestyle=':')
    plt.plot(x, dqn_delay, color='black', marker='D', linestyle='--')
    plt.plot(x, [fix_mean_delay for i in range(len(x))], color='gray', linestyle='--')
    plt.plot(x, [dqn_mean_delay for i in range(len(x))], color='black', linestyle='--')
    # 设置坐标轴名称
    plt.xlabel("信号周期", fontproperties="SimSun", size=10.5)
    plt.ylabel("延误时间(s)", fontproperties="SimSun", size=10.5)
    # 设置图例
    legend = ["Fix Dalay", "DQN Dalay", "Fix Mean Dalay", "DQN Mean Dalay"]
    plt.legend(legend, loc="best", frameon=False)
    # 保存图片
    plt.savefig(compar_img, dpi=600)
    # 关闭绘图
    plt.close()